package org.bankabeule.ui.utils;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;

/**
 * Wrapper to adapt the ArrayContentProvider to a TreeViewer
 */
public class ListArrayContentProvider extends ArrayContentProvider implements ITreeContentProvider {

	
	/**
	 * Indicate if list must be a tree or a flat list
	 */
    private boolean flat;


    /**
     * Default constructor for a tree content
     */
    public ListArrayContentProvider() {
        flat = true;
    }


    /**
     * Constructor for choosing between list or tree
     * @param flat
     */
    public ListArrayContentProvider(boolean flat) {
        this.flat = flat;
    }



    /**
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
     */
    public Object [] getChildren(Object parentElement) {
        if (parentElement instanceof EObject) {
            return ((EObject) parentElement).eContents().toArray();
        }
        return new Object [0];
    }



    /**
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
     */
    public Object getParent(Object element) {
        if (element instanceof EObject) {
            return ((EObject) element).eContainer();
        }
        return new Object [0];
    }



    /**
     * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
     */
    public boolean hasChildren(Object element) {
    	if (!flat) {
    		return !((EObject) element).eContents().isEmpty();
    	} else {
    		return false;
    	}
    }
    
    
}
